/**
 * 
 */
package at.ac.tuwien.ifs.calendit.basis.gui;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

import at.ac.tuwien.ifs.calendit.basis.helper.Constants;
import at.ac.tuwien.ifs.calendit.dao.ITermin;
import at.ac.tuwien.ifs.calendit.domain.Termin;


/**
 * @author Pinar
 *
 */
public class TerminTableModel extends AbstractTableModel{
     
	private static final long serialVersionUID = 1601716830428109276L;
		private List <Termin> termin = null;
        private Termin t;
        private XmlBeanFactory xbf = null;
        private  ITermin terminDao = null;
        private ClassPathResource res = null;
		/**
		 * Order of the TableModel, must be "Deadline".
		 */

        private final String[] colnames = {"Termin ID","Termin Typ", "Mitarbeiter ID", "Bezeichnung","Beschreibung","Enddatum","Beginndatum","End Uhrzeit","Beginn Uhrzeit","Wiederholung","Reminder","Meetingtermin","RaumID"};
        
        public TerminTableModel(){

			super();
			initDAO();
            getTerminData();
        }
        private void getTerminData(){
                termin = terminDao.getAll();
                }

        public int getCoumnCount(){
                return colnames.length;
        }
        public int getRowCount(){
                return termin.size();
        }
        public String getCoumnName(int c){
                return colnames[c];
        }
        
        public Termin insert(Termin t){
        	return terminDao.add(t);
        }
        public Termin getTerminToTermintyp(int typ){
                t= terminDao.getById(typ);
                fireTableDataChanged();
                return t;
        }
        
    	

        public Object getValueAt(int row, int col){
                Termin t = termin.get(row);
                switch(col){
                case 0 :
                        return ""+ t.getId();
                case 1: 
                        return t.getTermintyp();
                case 2:
                        return t.getBeschreibung();
                case 3: 
                        return t.getBezeichnung();
                case 4:
                        return t.getBeginnDatum();
                case 5:
                        return t.getEndDatum();
                case 6:
                        return t.getBeginnUhrzeit();
                case 7:
                        return t.getEndUhrzeit();
                case 8:
                        return t.getWiederholung();
                case 9:
                        return t.getReminder();
                case 10:
                	return t.getMeetingtermin();
                case 11:
                	      return t.getRaumID();
                default:
                        return null;
                                }
        }
        public Termin getTerminToId(int i){
                return termin.get(i);
        }
        public void reload(){
                getTerminData();
                fireTableDataChanged();
        }
        
        private void initDAO(){

              res = new ClassPathResource(Constants.SPRINGBEANS);
              xbf = new XmlBeanFactory(res);
              terminDao = (ITermin) xbf.getBean("TerminDAO");
        }
        public int getColumnCount() {
                // TODO Auto-generated method stub
                return 0;
        }

		public ITermin getTerminDao() {
			return terminDao;
		}
		public void setTerminDao(ITermin terminDao) {
			this.terminDao = terminDao;
		}
		
		public Termin getPropToTerminTime(Termin t){
			return terminDao.getIdToTerminTime(t);
		}
		
		public Termin getTerminOfRaum(Termin t){
			return terminDao.getRaumTermin(t);
		} 
		
		public Termin getTerminOfDay(Termin t){
			return terminDao.getRaumTermin(t);
		}
		
		public boolean deleteById(int id){
			return terminDao.delete(id);
		}

}
